package com.tuoyu.peopletest.utils;

import com.tuoyu.peopletest.common.MyApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class SettingsClass {
    //MainActivity
    private static int main_brightness = 560;   //亮度
    private static int main_start = 60; //开始
    private static int main_stop = 160; //结束
    private static boolean main_peak = true; //是否荧光（激发光，是峰值）
    private static int main_ad_position = 1; //读AD的孔位，1或2
    //MainActivityTest
    private static int maintest_frequency = 50; //采样频率
    private static int maintest_gain = 1;//采样增益
    private static int maintest_led = 2;//0-365 1-470 2-525
    //MainActivityRED 红光测试界面
    private static int mainred_brightness = 560; //亮度
    private static int mainred_start = 50; //开始
    private static int mainred_stop = 170; //结束

    static String mstrFilename = "config.txt";

    public static int getMainBrightness() {
        return main_brightness;
    }

    public static void setMainBrightness(int brightness) {
        main_brightness = brightness;
    }

    public static int getMainStart() {
        return main_start;
    }

    public static void setMainStart(int start) {
        main_start = start;
    }

    public static int getMainStop() {
        return main_stop;
    }

    public static void setMainStop(int stop) {
        main_stop = stop;
    }

    public static boolean getMainPeak() {
        return main_peak;
    }

    public static void setMainPeak(boolean bPeak) {
        main_peak = bPeak;
    }

    public static int getMainADPosition() {
        return main_ad_position;
    }

    public static void setMainADPosition(int adPosition) {
        main_ad_position = adPosition;
    }

    public static int getMainTestFrequency() {
        return maintest_frequency;
    }

    public static void setMainTestFrequency(int frequency) {
        maintest_frequency = frequency;
    }

    public static int getMainTesADGain() {
        return maintest_gain;
    }

    public static void setMainTestADGain(int adGain) {
        maintest_gain = adGain;
    }

    public static int getMainTestLED() {
        return maintest_led;
    }

    public static void setMainTestLED(int led) {
        maintest_led = led;
    }

    public static int getMainRedBrightness() {
        return mainred_brightness;
    }

    public static void setMainRedBrightness(int brightness) {
        mainred_brightness = brightness;
    }

    public static int getMainRedStart() {
        return mainred_start;
    }

    public static void setMainRedStart(int start) {
        mainred_start = start;
    }

    public static int getMainRedStop() {
        return mainred_stop;
    }

    public static void setMainRedStop(int stop) {
        mainred_stop = stop;
    }

    static File getConfigFile() {
        File dir = MyApplication.myApp.getFilesDir();
        File file = new File(dir, mstrFilename);
        return file;
    }

    public static void getSettings() {
        File configFile = getConfigFile();
        if (configFile == null)
            return;
        Properties properties = new Properties();
        try {
            FileInputStream s = new FileInputStream(configFile);
            properties.load(s);
            s.close();
            main_brightness = Integer.parseInt((String) properties.get("main_brightness"));
            main_start = Integer.parseInt((String) properties.get("main_start"));
            main_stop = Integer.parseInt((String) properties.get("main_stop"));
            main_peak = Boolean.parseBoolean((String) properties.get("main_peak"));
            main_ad_position = Integer.parseInt((String) properties.get("main_ad_position"));

            maintest_frequency = Integer.parseInt((String) properties.get("maintest_frequency"));
            maintest_gain = Integer.parseInt((String) properties.get("maintest_gain"));
            maintest_led = Integer.parseInt((String) properties.get("maintest_led"));

            mainred_brightness = Integer.parseInt((String) properties.get("mainred_brightness"));
            mainred_start = Integer.parseInt((String) properties.get("mainred_start"));
            mainred_stop = Integer.parseInt((String) properties.get("mainred_stop"));
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }

    public static void saveSettings() {
        File configFile = getConfigFile();
        if (configFile == null)
            return;
        try {
            Properties properties = new Properties();
            properties.setProperty("main_brightness", String.valueOf(main_brightness));
            properties.setProperty("main_start", String.valueOf(main_start));
            properties.setProperty("main_stop", String.valueOf(main_stop));
            properties.setProperty("main_peak", String.valueOf(main_peak));
            properties.setProperty("main_ad_position", String.valueOf(main_ad_position));

            properties.setProperty("maintest_frequency", String.valueOf(maintest_frequency));
            properties.setProperty("maintest_gain", String.valueOf(maintest_gain));
            properties.setProperty("maintest_led", String.valueOf(maintest_led));

            properties.setProperty("mainred_brightness", String.valueOf(mainred_brightness));
            properties.setProperty("mainred_start", String.valueOf(mainred_start));
            properties.setProperty("mainred_stop", String.valueOf(mainred_stop));

            FileOutputStream out = new FileOutputStream(configFile);
            properties.store(out, "");
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }
}
